#ifndef FE2288D6_D1E6_493E_8F79_1F4634ACF203
#define FE2288D6_D1E6_493E_8F79_1F4634ACF203


#endif /* FE2288D6_D1E6_493E_8F79_1F4634ACF203 */

// 第六步：制定协议

#include <iostream>
#include <string>

const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";

std::string Encode(std::string &content)    //封装字符串
{
    std::string package = std::to_string(content.size());
    package += protocol_sep;
    package += content;
    package += protocol_sep;

    return package;
}

// 将字符串打散
// "len"\n"x op y"\n
bool Decode(std::string &package, std::string *content)
{
    size_t pos = package.find(protocol_sep);
    if(pos == std::string::npos) return false;
    std::string len_str = package.substr(0,pos);
    size_t len = len_str.size();

    // 验证：package = len_str + len + 2
    size_t total_len = len_str.size() + len +2;
    if(package.size() < total_len) return false;

    *content = package.substr(pos+1,len);
    // erase需要移除报文， package.erase(total_len)
}

class Request
{
public:
    Request(int x, int y, char oper)
        :left_d(x),right_d(y),op(oper)
    {}
    Request()
    {}
    ~Request()
    {}
public:
    bool Serialize(std::string *out)        // 序列化
    {
        // 构建报文的有效载荷
        // struct -> string "left op right"
        std::string s = std::to_string(left_d);
        s += blank_space_sep;
        s += op;
        s += blank_space_sep;
        s += std::to_string(right_d);

        *out = s;
        return true;
    }
    bool Deserialize(const std::string &in)      // 反序列化
    {
        // string -> struct
        // "left op right" -> 
        size_t left = in.find(blank_space_sep);
        if(left == std::string::npos)  return false;
        std::string part_left = in.substr(0, left);

        size_t right = in.rfind(blank_space_sep);
        if(right == std::string::npos)  return false;
        std::string part_right = in.substr(right+1);

        if(left + 2 != right)   return false;
        op = in[left+1];
        left_d = std::stoi(part_left);
        right_d = std::stoi(part_right);

        return true;
    }

    void DebugPrint()
    {
        std::cout << "新请求构建完毕：" << left_d << op << right_d << "=?" << std::endl;
    }

public:
    int left_d;
    int right_d;
    char op;
};

class Response
{
public:
    Response(int res, int c)
        :result(res),code(c)
    {}
    Response()
    {}
    ~Response()
    {}
public:
    bool Serialize(std::string *out)        // 序列化
    {
        // 构建报文的有效载荷
        // struct -> string "result code"
        std::string s = std::to_string(result);
        s += blank_space_sep;
        s += std::to_string(code);

        *out = s;
        return true;
    }
    bool Deserialize(const std::string &in)      // 反序列化
    {
        // string -> struct
        // "result code" -> 
        size_t pos = in.find(blank_space_sep);
        if(pos == std::string::npos) return false;
        std::string part_left = in.substr(0, pos);
        std::string part_right = in.substr(pos+1);

        result = std::stoi(part_left);
        code = std::stoi(part_right);
        return true;
    }

    void DebugPrint()
    {
        std::cout << "结果相应完毕：result" << result << ", code: " << code << std::endl;
    }
public:
    int result;
    int code;       // 0表示结果正常，非0表示出现错误
};


